package com.loie.xcx.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.aop.SignatureValidation;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.comm.SeqEnum;
import com.loie.xcx.model.bo.*;
import com.loie.xcx.model.dto.*;
import com.loie.xcx.service.*;
import com.loie.xcx.util.SeqUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * @author zhl
 */
@Slf4j
@RequestMapping("/order")
@RestController
@AllArgsConstructor
public class QuotationController {
    private final ItemService itemService;
    private final SystemService systemService;
    private final QuotationService quotationService;
    private final CustomerService customerService;
    private final PermissionService permissionService;


    @SignatureValidation
    @PostMapping("/getItemInfoByItemNo")
    public JsonResult<ItemDTO> getItemInfoByItemNo(@RequestBody String jsonStr) {
        ItemBO itemBO = JSONObject.parseObject(jsonStr, ItemBO.class);
        if (StringUtils.isNotBlank(itemBO.getItemNo())) {
            itemBO = itemService.getItemById(itemBO.getItemNo());
            ItemDTO itemDTO = new ItemDTO();
            if (itemBO != null) {
                BeanUtils.copyProperties(itemBO, itemDTO);
                if (itemBO.getPhoto() != null) {
                    itemDTO.setPhoto(Base64.getEncoder().encodeToString(itemBO.getPhoto()));
                }
            }
            return new JsonResult<>(itemDTO);
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getItemInfoByItemNoandPermissions")
    public JsonResult<ItemDTO> getItemInfoByItemNoandPermissions(@RequestBody String jsonStr) {
        ItemBO itemBO = JSONObject.parseObject(jsonStr, ItemBO.class);
        String openId= itemBO.getOpenId();
        if (StringUtils.isNotBlank(itemBO.getItemNo())&& StringUtils.isNotBlank(openId)) {
            String moduleName = "产品资料";
            List<PermissionBO> permissionBOList = permissionService.getPermission(openId, moduleName);
            System.out.println(permissionBOList);
            if (permissionBOList.size() != 0) {
                itemBO = itemService.getItemByIdandPermissions(permissionBOList,itemBO.getItemNo());

                //list = customerService.getCustomersByPermissions(permissionBOList, customerDTO);
            } else {
                //list = new ArrayList<>();
                itemBO=null;
                return new JsonResult<>(400, "无相应的产品资料权限");
            }
            //itemBO = itemService.getItemById(itemBO.getItemNo());
            ItemDTO itemDTO = new ItemDTO();
            if (itemBO != null) {
                BeanUtils.copyProperties(itemBO, itemDTO);
                if (itemBO.getPhoto() != null) {
                    itemDTO.setPhoto(Base64.getEncoder().encodeToString(itemBO.getPhoto()));
                }
            }
            return new JsonResult<>(itemDTO);
        }
        return new JsonResult<>(400, "查询失败");
    }


    @SignatureValidation
    @PostMapping("/getItemByPermissionslist")
    public JsonResult<JSONObject> getItemByPermissionslist(@RequestBody String jsonStr) {
        QuotationQueryDTO quotationQueryDTO =  JSONObject.parseObject(jsonStr, QuotationQueryDTO.class);
        String openId= quotationQueryDTO.getOpenId();
        if ( StringUtils.isNotBlank(openId)&& quotationQueryDTO.getPageIndex() != null) {
            String moduleName = "产品资料";
            List<PermissionBO> permissionBOList = permissionService.getPermission(openId, moduleName);
            //System.out.println(permissionBOList);
            List<ItemBO> itemBOList;
            if (permissionBOList.size() != 0) {
                 itemBOList = itemService.getItemByPermissionslist(permissionBOList,quotationQueryDTO);

                //list = customerService.getCustomersByPermissions(permissionBOList, customerDTO);
            } else {
                //list = new ArrayList<>();
                //itemBO=null;
                return new JsonResult<>(400, "无相应的产品资料权限");
            }
            //itemBO = itemService.getItemById(itemBO.getItemNo());
            List<ItemDTO> list=new ArrayList<ItemDTO>();
            //ItemDTO itemDTO = new ItemDTO();
            for (ItemBO itemBO:itemBOList) {
                if (itemBO != null) {
                    ItemDTO itemDTO = new ItemDTO();
                    BeanUtils.copyProperties(itemBO, itemDTO);
                    if (itemBO.getPhoto() != null) {
                        itemDTO.setPhoto(Base64.getEncoder().encodeToString(itemBO.getPhoto()));
                    }
                    list.add(itemDTO);
                }
            }
            JSONObject jsonresult = new JSONObject();
            jsonresult.put("Total",list.size());
            jsonresult.put("itemlist",list);
            return new JsonResult<>(jsonresult);
        }
        return new JsonResult<>(400, "查询失败");
    }

    private final static String OP_TYPE_ADD = "0";
    private final static String OP_TYPE_UPDATE = "1";
    private final static String OP_TYPE_DELETE = "2";

    private final SeqUtil seqUtil;

    @SignatureValidation
    @PostMapping("/putQuotation")
    public JsonResult<QuotationResultDTO> putOrder(@RequestBody String jsonStr) {
        QuotationCommitDTO quotationCommitDTO = JSONObject.parseObject(jsonStr, QuotationCommitDTO.class);
        String opType = quotationCommitDTO.getOpType();
        String openId = quotationCommitDTO.getOpenId();
        UserInfoBO userInfoBO = systemService.getUserInfoByOpenId(openId);
        if (StringUtils.isNotBlank(opType)) {
            QuotationBO quotationBO = new QuotationBO();
            BeanUtils.copyProperties(quotationCommitDTO, quotationBO);
            List<QuotationLineBO> list;
            switch (opType) {
                case OP_TYPE_ADD:
                    quotationBO.setUserId(userInfoBO.getUserId());
                    String quotationNo = seqUtil.getSeq(SeqEnum.XCXBJ, "yyMM", 4);
                    quotationBO.setQuotationNo(quotationNo);
                    list = quotationCommitDTO.getItemList();
                    String recordId = quotationService.addQuotation(quotationBO, list);
                    QuotationResultDTO dto = new QuotationResultDTO();
                    dto.setQuotationNo(quotationNo);
                    dto.setRecordId(recordId);
                    return new JsonResult<>(dto);
                case OP_TYPE_UPDATE:
                    quotationBO.setUserId(userInfoBO.getUserId());
                    list = quotationCommitDTO.getItemList();
                    quotationService.updateQuotation(quotationBO, list);
                    return new JsonResult<>();
                case OP_TYPE_DELETE:
                    quotationService.deleteQuotation(quotationBO);
                    return new JsonResult<>();
                default:
                    throw new IllegalStateException("Unexpected value: " + opType);
            }
        }
        return new JsonResult<>(400, "执行失败");
    }

    @SignatureValidation
    @PostMapping("/getQuotationList")
    public JsonResult<QuotationListResultDTO> getQuotationList(@RequestBody String jsonStr) {
        QuotationQueryDTO quotationQueryDTO = JSONObject.parseObject(jsonStr, QuotationQueryDTO.class);
        if (StringUtils.isNotBlank(quotationQueryDTO.getKind()) && quotationQueryDTO.getPageIndex() != null) {
            List<QuotationBO> quotationList = quotationService.getQuotationList(quotationQueryDTO);
            int size = quotationService.getQuotationListTotal(quotationQueryDTO);
            QuotationListResultDTO quotationListResultDTO = new QuotationListResultDTO();
            quotationListResultDTO.setItemList(quotationList);
            quotationListResultDTO.setTotal(size);
            return new JsonResult<>(quotationListResultDTO);
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getQuotationLineList")
    public JsonResult<QuotationLineListResultDTO> getQuotationLineList(@RequestBody String jsonStr) {
        QuotationQueryDTO quotationQueryDTO = JSONObject.parseObject(jsonStr, QuotationQueryDTO.class);
        if (StringUtils.isNotBlank(quotationQueryDTO.getRecordId()) && quotationQueryDTO.getPageIndex() != null) {
            QuotationBO quotationBO = quotationService.getQuotation(quotationQueryDTO);
            if (quotationBO != null) {
                QuotationLineListResultDTO quotationLineListResultDTO = new QuotationLineListResultDTO();
                BeanUtils.copyProperties(quotationBO, quotationLineListResultDTO);
                List<QuotationLineBO> quotationList = quotationService.getQuotationLineList(quotationQueryDTO);
                int size = quotationService.getQuotationLineListTotal(quotationQueryDTO);
                quotationLineListResultDTO.setItemList(quotationList);
                quotationLineListResultDTO.setTotal(size);
                return new JsonResult<>(quotationLineListResultDTO);
            }
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getQuotationLine")
    public JsonResult<QuotationLineDTO> getQuotationLine(@RequestBody String jsonStr) {
        QuotationLineQueryDTO quotationLineQueryDTO = JSONObject.parseObject(jsonStr, QuotationLineQueryDTO.class);
        if (StringUtils.isNotBlank(quotationLineQueryDTO.getRecordId()) && StringUtils.isNotBlank(quotationLineQueryDTO.getParentId())) {
            QuotationLineBO quotationLineBO = quotationService.getQuotationLine(quotationLineQueryDTO);
            QuotationLineDTO quotationLineDTO = new QuotationLineDTO();
            if (quotationLineBO != null) {
                BeanUtils.copyProperties(quotationLineBO, quotationLineDTO);
                if (quotationLineBO.getPhoto() != null) {
                    quotationLineDTO.setPhoto(Base64.getEncoder().encodeToString(quotationLineBO.getPhoto()));
                }
            }
            return new JsonResult<>(quotationLineDTO);
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getQuotationLinePhoto")
    public JsonResult<JSONObject> getQuotationLinePhoto(@RequestBody String jsonStr) {
        QuotationLineQueryDTO quotationLineQueryDTO = JSONObject.parseObject(jsonStr, QuotationLineQueryDTO.class);
        if (StringUtils.isNotBlank(quotationLineQueryDTO.getRecordId()) && StringUtils.isNotBlank(quotationLineQueryDTO.getParentId())) {
            QuotationLineBO quotationLineBO = quotationService.getQuotationLinePhoto(quotationLineQueryDTO);
            JSONObject jsonObject = new JSONObject();
            if (quotationLineBO.getPhoto() != null) {
                jsonObject.put("photo", Base64.getEncoder().encodeToString(quotationLineBO.getPhoto()));
            }
            return new JsonResult<>(jsonObject);
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getCustomerInfo")
    public JsonResult<List<CustomerBO>> getCustomerInfo(@RequestBody String jsonStr) {
        CustomerBO customerBO = JSONObject.parseObject(jsonStr, CustomerBO.class);
        if (StringUtils.isNotBlank(customerBO.getCustomerCorpName())) {
            List<CustomerBO> list = customerService.getCustomersByCorpName(customerBO.getCustomerCorpName());
            return new JsonResult<>(list);
        }
        return new JsonResult<>(400, "查询失败");
    }

}
